/*
 * Author		: Soholife
 * Description 	: 日期相关工具类
 * Date			: 2015-05-23
 */

package com.easycode.devtool.util;


import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

final public class DateUtils {

    /**
     * 获取当月第一天的日期
     * @return
     */
    public static Date getCurrentMonthFirstDay()
    {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,1);
        return calendar.getTime();
    }
    /**
     * 获取当月第一天字符串
     *
     * @return
     */
    public static String getCurrentMonthFirstDayStr(){

        return new SimpleDateFormat("yyyy-MM-dd").format(getCurrentMonthFirstDay());
    }

    /**
     * 获取当前月的最后一天的日期
     * @return
     */
    public static  Date getCurrentMothLastDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 获取当月最后一天字符串
     *
     * @return
     */
    public static String getCurrentMonthLastDayStr(){

        return new SimpleDateFormat("yyyy-MM-dd").format(getCurrentMothLastDay());
    }




	/*
     * 日期转换为字符串
     */
    public static  String  DateTime2String(Date date)
    {
    	return Date2String(date,"yyyy-MM-dd HH:mm:ss SSS");
    }
    public static String  Date2String(Date date)
    {
    	return Date2String(date, "yyyy-MM-dd");
    }
    public static String  Date2String(Date date,String partten)
    {
    	SimpleDateFormat df = new SimpleDateFormat(partten);    	
    	return 	df.format(date);
    }
    
    
    /*
     * 字符串转换为日期
     */
    public static Date String2DateTime(String str)    
    {
    	return String2Date(str, "yyyy-MM-dd HH:mm:ss SSS", new Date());
    }
    public static Date String2Date(String str)
    {
    	return String2Date(str,"yyyy-MM-dd",new Date());
    }
    public static Date String2Date(String str,String partten,Date defaultValue)
    {
    	//java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("M/dd/yyyy hh:mm:ss a");
    	//java.util.Date d = sdf.parse("5/13/2003 10:31:37 AM");
    	try
    	{
    		SimpleDateFormat sdf = new SimpleDateFormat(partten);
    		return  sdf.parse(str);
    	}
    	catch(Exception e)
    	{
    		return defaultValue;
    	}
    }

//    public static DateModel getYesterdayModel(String dateString)
//    {
//        return  getYesterdayModel(dateString,"yyyy-MM-dd");
//    }
//
//    public static DateModel getYesterdayModel(String dateString, String dateFormate)
//    {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormate);
//        Date date = null;
//        try {
//            date = simpleDateFormat.parse(dateString);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        return getYesterdayModel(date);
//
//    }
//
//
//    public static DateModel getYesterdayModel(Date date)
//    {
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(date);
//        return getYesterdayModel(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1,calendar.get(Calendar.DAY_OF_MONTH));
//    }
//
//
//
//    public static DateModel getYesterdayModel(DateModel dateModel)
//    {
//        return  getYesterdayModel(dateModel.getYear(),dateModel.getMonth(),dateModel.getDay());
//    }
//    /**
//     * 获取某日期的昨天的日期模型
//     * @param year  年
//     * @param month  月
//     * @param day  日
//     * @return
//     */
//    public static DateModel getYesterdayModel(int year, int month , int day)
//    {
//       Calendar todayCalendar = Calendar.getInstance();
//       todayCalendar.set(year,month-1,day);
//       todayCalendar.add(Calendar.DATE,-1);
//
//        DateModel dateModel =  new DateModel(todayCalendar.get(Calendar.YEAR),todayCalendar.get(Calendar.MONTH)+1,todayCalendar.get(Calendar.DAY_OF_MONTH));
//        return  dateModel;
//    }



    public static Date getYesterday()
    {
        return addDate("day",-1,getNowDate());
    }

    public static Date getTheDayBeforeYesterday()
    {
        return addDate("day",-2,getNowDate());
    }


    public static Date addDay(int dayNum, Date date) {
        return addDate("day", dayNum, date);
    }
    /***
     *
     * @param partten 获取当前时间的格式
     * @return
     */
    public static Date getNowDate(String partten) {

        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(partten);
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(0);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }
    /**
     * 日期增加方法
     * @param type
     * @param num
     * @param date
     * @return
     */
    public static Date addDate(String type,int num, Date date)
    {
        int value = 0 ;
        if(type.equalsIgnoreCase("year"))
        {
            value = Calendar.YEAR;
        }
        if(type.equalsIgnoreCase("month"))
        {
            value = Calendar.MONTH;
        }
        if(type.equalsIgnoreCase("day"))
        {
            value = Calendar.DAY_OF_YEAR;
        }
        else if(type.equalsIgnoreCase("hour"))
        {
            value = Calendar.HOUR;
        }
        else if(type.equalsIgnoreCase("minute"))
        {
            value = Calendar.MINUTE;
        }
        else if(type.equalsIgnoreCase("second"))
        {
            value = Calendar.SECOND;
        }

        return  addDate( value, num,  date);
    }

    /**
     * 得到增加特定时间后的日期
     * @param type  Calendar.MINUTE一类的常量信息
     * @param num
     * @param date
     * @return
     */
    public static Date addDate(int type,int num, Date date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(type, num);
        return  calendar.getTime();
    }

    /**
     * 比较两个时间的大小
     * @param d1
     * @param d2
     * @return
     *  0 :表示时间相等
     *  1 :表示 d1 > d2 (d1时间晚于d2时间)
     *  -1:表示 d1 < d2 (d1时间早于d2时间)
     */
    public static int compare(Date d1,Date d2)
    {
        return  d1.compareTo(d2);
    }

    /**
     * 获取时间戳字符串信息
     * @return
     */
    public static String getTimestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }

    /**
     * 获取当前日期
     * @return
     */
    public static Date getNowDate()
    {
        return new Date(System.currentTimeMillis());
    }


    /**
     * 设置Date中的天数
     * @param day
     * @return
     */
    public static Date setDayOfMoth(int day, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, day);

        return calendar.getTime();
    }

    /**
     * 日期之间间隔的天数
     * @param startDate
     * @param endDate
     * @return
     */
    public static int daysOfTwoDate(Date startDate, Date endDate) {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startDate);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);

        return endCalendar.get(Calendar.DAY_OF_YEAR) - startCalendar.get(Calendar.DAY_OF_YEAR);
    }


    /**
     * 挂起当前程序
     * @param ms    程序挂起时间，单位：毫秒
     */
    public static void wait(int ms)
    {
        try {
            Thread.sleep(ms); //1000 毫秒，也就是1秒.
        }
        catch(InterruptedException ex)
        {
            //Thread.currentThread().interrupt();
        }
        catch (Exception e){}
        catch (Error e){}
    }

//    public static  void  main(String arg[])
//    {
////        Date d1=String2Date("2016-05-16");
////
////        Date d2=String2Date("2016-05-20");
////        while (d1.getTime() < d2.getTime()) {
////            long days = ((d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
////            System.out.println(days);
////            d1=DateUtils.addDate("day", 1, d1);
////        }
//
//
////        System.out.println(getCurrentMonthFirstDay());
////
////        DateModel dateModel =  getYesterdayModel(2017,3,2);
////        System.out.println(dateModel);
////        DateModel dateModel2 =  getYesterdayModel(dateModel);
////        System.out.println(dateModel2);
//
//        System.out.println(Date2String(getYesterday()));
//    }

}
